// 2014-02-01 Tyrghen
//---------------------------------------------
// Usage:
//  [] call edt_fnc_;
//---------------------------------------------
// Purpose:
// 
// 
// Parameters:
// - 
//
// Returns:
// 
//---------------------------------------------
// Objectif:
// 
//
// Paramètres:
// - 
//
// Renvoie:
// 
//---------------------------------------------

//#define __FORCE_TRACE__
//#define __FORCE_DEBUG__
//#define __FORCE_INFO__
#define THIS_FILE "editor\fn_conquer.sqf"
#include <tmf_constants.h>
#include <tmf_macros.h>

if (!isServer) exitWith {TRACE("Not Server")};

TRACE_1("Start",_this);
TMFPROFILERSTART;

private ["_type","_marker","_error","_obj_params","_obj_params","_conquered_var","_conquered_msg","_conquer_owner","_conquer_taker","_trg"];
DEFAULT_PARAM(_marker,0,"");
DEFAULT_PARAM(_conquered_var,1,"");
DEFAULT_PARAM(_conquered_msg,2,["STR_TMF_TARGET_CONQUERED"]);
DEFAULT_PARAM(_conquer_owner,3,[GVAR(side_enemy)]);

_obj = objNull;
_is_valid = true;

_type = "Land_HelipadEmpty_F";

if (AND_ALSO(IS_OBJECT(_marker),IS_TRIGGER(_marker))) then {
	_marker = [_marker] call FFUNC(triggerToMarker);
};
if (IS_STRING(_marker)) then {
	if (!IS_MARKER(_marker)) then {
		_trg = missionNameSpace getVariable _marker;
		if (!isnil "_trg") then {
			if (IS_TRIGGER(_trg)) then {
				_marker = [_trg] call FFUNC(triggerToMarker);
			};
		};
	};
	_is_valid = IS_MARKER(_marker);
};

if (AND_ALSO(IS_STRING(_type),IS_ARRAY(_marker))) then {
	_is_valid = count _marker < 2;
};
_conquered_var = _conquered_var call FFUNC(getStringVar);

if (_is_valid) then {
	_obj_params = [];
	_obj_params set [TMF_VEH_PARAMS_TYPE, _type];
	_obj_params set [TMF_VEH_PARAMS_POS, _marker];
	
	if (IS_ARRAY(_conquer_owner)) then {
		for "_i" from 0 to ((count _conquer_owner) - 1) step 1 do {
			_conquer_owner set [_i, ([_conquer_owner select _i] call FFUNC(getSide))];
		};
	} else {
		_conquer_owner = [([_conquer_owner select _i] call FFUNC(getSide))];
	};
	// If the owner has the enemy side in it, taker is own side. If the owner has own side in it, then taker is enemy side
	_conquer_taker = [GVAR(side_own)];
	if (GVAR(side_own) in _conquer_owner) then {
		_conquer_taker = [GVAR(side_enemy)];
		// Check if independents are enemies
		if (((resistance) getFriend GVAR(side_own)) < 0.6) then { _conquer_taker set [count _conquer_taker, resistance];};
	} else {
		// Check if independents are friendlies
		if (((resistance) getFriend GVAR(side_own)) >= 0.6) then { _conquer_taker set [count _conquer_taker, resistance];};
	};
	
	_tmf_params = [];
	_tmf_params = [_tmf_params,"CONQUER_MSG",_conquered_msg] call FFUNC(hashSet);
	_tmf_params = [_tmf_params,"CONQUER_ZONE",_marker] call FFUNC(hashSet);
	_tmf_params = [_tmf_params,"CONQUER_OWNER",_conquer_owner] call FFUNC(hashSet);
	_tmf_params = [_tmf_params,"SHARED_VAR",_conquered_var] call FFUNC(hashSet);
	_tmf_params = [_tmf_params,"CONQUER_LEFT",-1] call FFUNC(hashSet);
	// _tmf_params = [_tmf_params,"CONQUER_CODE",""] call FFUNC(hashSet);
	// _tmf_params = [_tmf_params,"CONQUER_TIMEOUT",30] call FFUNC(hashSet);
	_tmf_params = [_tmf_params,"CONQUER_TAKER",_conquer_taker] call FFUNC(hashSet);

	_obj = [_obj_params,"CONQUER",_tmf_params] call FFUNC(CreateMissionObject);
	TRACE_1("End",ARR_3(_obj, typeof _obj, getPos _obj));
} else {
	ERROR("Invalid Parameters");
};
TMFPROFILERSTOP;
_obj